//登録内容をデータベースに登録するクラス


package member;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import common.DBManager;

public class WriteServet extends HttpServlet {

	Statement smt  = null;
	Connection con = null;


	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();

		//各入力パラメータの取得
		String user_id 		= req.getParameter("user_id");
		String user_name	= req.getParameter("user_name");
		String user_ruby	= req.getParameter("user_ruby");
		String password	= req.getParameter("password");

//		String path = "/index.html";
//		登録後の遷移先を変更	2013.10.16	若林
//		String path = "/member/userMenu.jsp";
		String path = "/member/UserMemberSearchServlet";


		con = DBManager.getConnection();


		try {

			if(Duplicate(user_id)){
				path 		= "/member/userMemberEntryCheck.jsp";

			}else{
			//	問い合わせ（SQL）を実行する準備

			smt  = con.createStatement();

			String  isql = "insert into  members(USER_ID,PASSWORD,NAME,RUBY,ID) value(?,?,?,?,null)";

			PreparedStatement ist = con.prepareStatement(isql);

			ist.setString(1, user_id);
			ist.setString(2, password);
			ist.setString(3, user_name);
			ist.setString(4, user_ruby);

			ist.executeUpdate();

			if(smt != null){
				try{
					smt.close();
				}
				catch(Exception e){

				}
			if(con != null){
				try{
					con.close();
				}
				catch(Exception e){
				}

		}
			}
			}

		}catch (SQLException e){
			throw new ServletException(e);
		}finally{
			if(smt != null){
				try{smt.close();}catch(SQLException ignore){}
			}
			if(con != null){
				try{con.close();}catch(SQLException ignore){}
			}
	}

		String duplicate		= null;
		req.setAttribute("duplicate", duplicate);
		//	登録後に書籍検索結果の最終頁を表示するためにページ番号を書き換える		2013.10.16	若林
		//	セッション情報からパラメータを取得
		HttpSession s = req.getSession();
		@SuppressWarnings("unchecked")
		Map<String, String[]> m = (Map<String, String[]>)s.getAttribute("MemberSerchParameter");
		//	ページ番号を９９９９９９に書きかえる
		String[] pages = {"999999"};
		//	パラメータをセッション情報に保存
		m.put("page", pages);
		s.removeAttribute("MemberSerchParameter");
		s.setAttribute("MemberSerchParameter", m);
		//		ここまで


//		一覧へ遷移後のメッセージを追加	2013.10.17	矢野
		req.setAttribute("msg", "登録しました");

//　利用者検索結果画面へ遷移
		RequestDispatcher dispatcher = req.getRequestDispatcher(path);
		dispatcher.include(req, resp);
			}

public boolean Duplicate(String user_id) throws SQLException{

		boolean duplicate = false;
		System.out.println(duplicate);
	String id_sql = "select count(*) as cnt from members where user_id = '"+user_id + "'";

	con = DBManager.getConnection();
	smt  = con.createStatement();

	ResultSet rs   = smt.executeQuery(id_sql);

	if(rs.next()){
		int coung = rs.getInt("cnt");

		if(coung > 0){

			duplicate = true;
		}
	}

	return duplicate;
	}
}
